package com.pocketpets.main;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import com.app.pocketpets.R;
import com.pocketpets.globaldata.ItemListClass;
import com.pocketpets.model.map.CaughtItemListClass;

public class RockActivity extends Activity implements SensorEventListener {

	// ================================================================
	// constants
	// ================================================================

	private static final int UPDATE_FREQUENCY = 100;
	private static final int FORCE_THRESHOLD = 900;
	private static final int FORCE_FACTOR = 10000;

	// ================================================================
	// Fields
	// ================================================================

	private SensorManager mSensorMgr = null;
	private Sensor mSensor;
	private long mLastUpdate = -1;
	private float[] mCurrPos = new float[3];
	private float[] mLastPos = { 0, 0, 0 };

	private Timer mCountDown;
	private TextView mTimeLeft;
	private TextView mCountView;
	private int mCount = 0;

	// ================================================================
	// Methods inherited
	// ================================================================

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rock);
		mTimeLeft = (TextView) findViewById(R.id.rocktimer);
		mCountView = (TextView) findViewById(R.id.rockcount);

		final Handler handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				Log.i("TTTTTTTTTTimeimeimeime",""+msg.what);
				if (msg.what > 0)
					mTimeLeft.setText("Time Left: " + msg.what);
				else {
					if (mCount>=20)
					{
						mTimeLeft.setText("You Got It!");
						mCountDown.cancel();
					}
					else
					{
						mTimeLeft.setText("Times Up");
						mCountDown.cancel();
						CaughtItemListClass caughtList=CaughtItemListClass.getInstance();
						caughtList.getItemList().remove(caughtList.getItemList().size()-1);
					}
					
				}
			}
		};

		mCountDown = new Timer();
		mCountDown.schedule(new TimerTask() {
			int time = 30;

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Message msg = new Message();
				msg.what = time--;
				handler.sendMessage(msg);
			}
		}, 2000, 1000);
	}

	@Override
	protected void onResume() {
		mSensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
		mSensor = mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		if (!mSensorMgr.registerListener(this, mSensor,
				SensorManager.SENSOR_DELAY_GAME)) {
			mSensorMgr.unregisterListener(this, mSensor);
		}
		super.onResume();
	}

	@Override
	protected void onPause() {
		if (mSensorMgr != null) {
			mSensorMgr.unregisterListener(this, mSensor);
			mSensorMgr = null;
		}
		super.onPause();
	}

	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	public void onSensorChanged(SensorEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
			long curTime = System.currentTimeMillis();
			if (curTime - mLastUpdate < UPDATE_FREQUENCY)
				return;
			long diffTime = curTime - mLastUpdate;
			mLastUpdate = curTime;

			mCurrPos = arg0.values.clone();
			float force = Math.abs(mCurrPos[0] + mCurrPos[1] + mCurrPos[2]
					- mLastPos[0] - mLastPos[1] - mLastPos[2])
					/ diffTime * FORCE_FACTOR;
			if (force > FORCE_THRESHOLD) {
				++mCount;
				mCountView.setText("Number of Shake: " + mCount);
				if (mCount==20)
				{
					this.finish();
				}
			}
		}
	}

}
